// subPages/mine/share/share.js
// import share from '../../../utils/share';
var util =  require('../../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // template: {},
    curTab: 0,
    themeColor: app.globalData.themeColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  getData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/fx', //接口地址
      method: 'get',
      header: {
          'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.length!=0){
          that.setData({
            isShare: true,
            shareImageList: res.data
          })
        }
      },
      fail: function (res) {
        util.showModal('提示','读取分享图片失败')
      }
    })
  },

  //内容滚动
  switchTab(event){
    var that = this
    var cur = event.detail.current;
    that.setData({
      curTab: cur
    })
  },

  // onImgOK(e) {
  //   this.imagePath = e.detail.path;
  //   // console.log(e);
  // },

  // //阻止穿透
  // preventTouchMove: function () {
  //   // console.log('stop user scroll it!');
  //   return;
  // },

  // //保存图片
  // lickBtn:function(){
  //   var that = this
  //   that.setData({
  //     isShare: true,
  //     template: new share().palette(that.data.shareImageList[that.data.curTab].url),
  //   });
  // },

  lickBtn:function(){
    wx.getSetting({
      success(res) {
        // console.log(res)
        //没有授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope:'scope.writePhotosAlbum',
            success() {
              console.log('授权成功')
            }
          })
        }
      }
    })
    // console.log(this.data.shareImageList[this.data.curTab].url)
    var imgUrl = this.data.shareImageList[this.data.curTab].url
    wx.downloadFile({
      url: imgUrl,
      success:function(res){
        // console.log(res)
        let path = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success:function(res) { 
            util.showToast('保存图片成功','success')
          },
          fail: function (err) {
            // console.log(err)
            if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              wx.showModal({
                title: '提示',
                content: '请重新授权保存相册',
                showCancel: false,
                success:modalSuccess=>{
                  wx.openSetting({
                    success(settingdata) {
                      // console.log("settingdata", settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        util.showModal('提示','获取权限成功,再次点击图片即可保存')
                      } else {
                        util.showModal('提示','获取权限失败，将无法保存到相册哦~')
                      }
                    },
                    fail(err) {
                      // console.log("err",err)
                    }
                  })
                }
              })
            }
          },
        })
      },fail:function(res){
        console.log(res)
      }
    })
    
  },

  // close:function(){
  //   this.setData({
  //     isShare: false,
  //     template: {},
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})